import Vue from 'vue'
import VueRouter from 'vue-router'


import home from '@/views/home/home.vue'
import login from '@/views/login/login.vue'
import user from '@/views/user/user.vue'
import article from '@/views/article/article.vue'
import writeArticle from '@/views/article/writeArticle.vue'
import Cookies from 'js-cookie'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'home',
    meta: {
      title: '首页'
    },
    component: home
  },
  {
    path: '/login',
    name: 'login',
    meta: {
      title: '登录'
    },
    component: login
  },
  {
    path: '/user',
    name: 'user',
    meta: {
      title: '用户管理'
    },
    component: user
  },
  {
    path: '/article',
    name: 'article',
    meta: {
      title: '发布文章'
    },
    component: article
  },
  {
    path: '/writeArticle',
    name: 'writeArticle',
    meta: {
      title: '写文章'
    },
    component: writeArticle
  }
]

const router = new VueRouter({
  routes
})


router.beforeEach((to, from, next) => {
  let token = Cookies.get('myToken')
  if(token || to.name=='login') {
    next()
  }else {
    next('/login')
  }
})



export default router
